<!--新增短信模板-->
<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <el-form ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="介绍">
          <el-input v-model="detail.introduce" />
        </el-form-item>
        <el-form-item label="套餐名称">
          <el-input v-model="detail.name" />
        </el-form-item>
        <el-form-item label="套餐类型">
          <el-select v-model="detail.type" placeholder="请选择">
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="detail.remarks" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="detail.sort" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { kitAdd, kitUpdate } from '@/api/api'
export default {
  data() {
    return {
      fileList: [],
      typeOptions: [
        {
          value: '1',
          label: '会员套餐'
        },
        {
          value: '2',
          label: '畅聊套餐'
        }],
      detail: {
        'introduce': '',
        'name': '',
        'remarks': '',
        'sort': 0,
        'type': ''
      }
    }
  },
  created() {
    if (this.$route.query.item) {
      this.detail = JSON.parse(this.$route.query.item)
    }
  },
  methods: {
    handleRemove(index) {
      this.fileList.splice(index, 1)
    },
    // 上传图片之前的验证
    handleUpload() {
    },
    // 上传成功之后的回调
    handleSuccess(res) {
      if (res.code === 200) {
        this.fileList.push(res.data)
        this.detail.imgLink = this.fileList[0]
      }
    },
    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      // if (!this.vertify()) {
      //   return false
      // }
      if (this.$route.query.item) {
        // 编辑
        kitUpdate(this.detail).then(data => {
          Is.tip('编辑成功', 2000, 'success')
          this.$router.back()
        })
      } else {
        // 新增
        kitAdd(this.detail).then(data => {
          Is.tip('添加成功', 2000, 'success')
          this.$router.back()
        })
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      if (Is.empty(this.detail.title)) {
        Is.tip('请输入标题')
        return false
      }
      if (Is.empty(this.detail.link)) {
        Is.tip('请输入链接')
        return false
      }
      if (Is.empty(this.detail.showType)) {
        Is.tip('请输入显示位置')
        return false
      }
      if (Is.empty(this.detail.sort)) {
        Is.tip('请输入排序')
        return false
      }
      if (Is.empty(this.fileList)) {
        Is.tip('请上传图片')
        return false
      }
      return true
    }
  }}
</script>
